/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.csv.enumeration;

import java.util.regex.Pattern;

import com.genia.toolbox.constants.RegularExpressions;
import com.genia.toolbox.projects.csv.bean.FieldDescriptor;

/**
 * the different type of fields.
 */
public enum FieldType
{

  /**
   * the default field: no verification whatsoever is done.
   */
  DEFAULT(".*"),
  /**
   * the field must be an integer.
   */
  INTEGER(RegularExpressions.INTEGER),
  /**
   * the field must be a decimal.
   */
  DECIMAL(RegularExpressions.DECIMAL),
  /**
   * the field represents an i18n value.
   */
  I18N(".*")
  {
    /**
     * returns the number of cvs columns that this field use.
     * 
     * @param fieldDescriptor
     *          the {@link FieldDescriptor} describing the field being tested.
     * @return the number of cvs columns that this field use
     */
    @Override
    public int getFieldSize(FieldDescriptor fieldDescriptor)
    {
      return fieldDescriptor.getFolderDescriptor().getMappingDescriptor().getSupportedLocales().size();
    }
  },
  /**
   * the field represents an i18n value that is used only for binding.
   */
  I18N_BIND(".*"),
  /**
   * the field represents a date.
   */
  DATE(".*"),
  /**
   * the field represents a date and a time.
   */
  DATETIME(".*"),
  /**
   * the field represents a time.
   */
  TIME(".*"),
  /**
   * the field represents a boolean.
   */
  BOOLEAN("(true)|(false)"),
  /**
   * the field represents a file to upload.
   */
  FILE("[^|]+(|[^|]+(|[^|]+)?)?"),
  /**
   * the field represents a directory to upload.
   */
  DIRECTORY(".+");

  /**
   * the {@link Pattern} that the field values must match.
   */
  protected final Pattern regularExpression;



  /**
   * constructor.
   * 
   * @param regularExpression
   *          the regular expression that the field values must match.
   */
  private FieldType(String regularExpression)
  {
    this.regularExpression = Pattern.compile(regularExpression);
  }



  /**
   * return <code>true</code> if and only if the value of the field is correct
   * for the current type of field.
   * 
   * @param fieldDescriptor
   *          the {@link FieldDescriptor} describing the field being tested.
   * @param fieldValue
   *          the value to check
   * @return <code>true</code> if and only if the value of the field is
   *         correct for the current type of field
   */
  public boolean isFieldCorrect(FieldDescriptor fieldDescriptor, String fieldValue)
  {
    return regularExpression.matcher(fieldValue).matches();
  }



  /**
   * returns the number of cvs columns that this field use.
   * 
   * @param fieldDescriptor
   *          the {@link FieldDescriptor} describing the field being tested.
   * @return the number of cvs columns that this field use
   */
  public int getFieldSize(FieldDescriptor fieldDescriptor)
  {
    return 1;
  }
}
